package com.wang.blog.service.impl;

import com.wang.blog.dao.SysUserDAO;
import com.wang.blog.model.SysUser;
import com.wang.blog.model.SysUserExample;
import com.wang.blog.service.UserService;
import com.wang.blog.util.PasswordHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Random;

/**
 * @author shimh
 * <p>
 * 2018年1月23日
 */
@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private SysUserDAO sysUserDAO;

    @Override
    public List<SysUser> findAll() {
        return null;
    }

    @Override
    public SysUser getUserByAccount(String account) {
        SysUserExample sysUserExample=new SysUserExample();
        SysUserExample.Criteria criteria=sysUserExample.createCriteria();
        criteria.andAccountEqualTo(account);
        List<SysUser> list= sysUserDAO.selectByExample(sysUserExample);
        return list.size()>0?list.get(0):null;
    }

    @Override
    public SysUser getUserById(Long id) {

        return sysUserDAO.selectByPrimaryKey(id);
    }

    @Override
    public Long saveUser(SysUser user) {
        PasswordHelper.encryptPassword(user);
        int index = new Random().nextInt(6) + 1;
        String avatar = "/static/user/user_" + index + ".png";

        user.setAvatar(avatar);
        return new Integer(sysUserDAO.insert(user)).longValue();
    }


    @Override
    public Long updateUser(SysUser user) {
        return null;
    }

    @Override
    public void deleteUserById(Long id) {

    }

}
